//
//  ZYCCircleFriendsViewController.m
//  ZDSC
//
//  Created by ZDTX on 2021/9/19.
//

#import "ZYCCircleFriendsViewController.h"
#import "ZYCCycleFriendModel.h"
#import "ZYCCircleFriendsCell.h"
#import "ZYCNoDataView.h"
#import "ZYCCicleFriendsDetailPhotoViewController.h"
#import "ZYCCicleFriendsPlainDetailTextViewController.h"
#import "ZYCGrowthFileHeaderView.h"
#import "ZYCPublishCircleFriendsViewController.h"
#import "ZYCNavigationController.h"
#import "ZYCGrowthFileModel.h"
#import "UITableView+NoData.h"
#import "NSDateExtra.h"
static NSInteger pageSize = 10;
@interface ZYCCircleFriendsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ZYCGrowthFileHeaderView *headerView;
@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) ZYCNoDataView *noDataView;

@end

@implementation ZYCCircleFriendsViewController
#pragma mark - 生命周期函数
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = self.titleString;
    [self.view addSubview:self.tableView];
    self.tableView.tableHeaderView = self.headerView;
    self.headerView.model = self.model;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self loadNewData];
}


#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYCCircleFriendsCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCCircleFriendsCell class]) forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.section];
    return cell;
}
-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 15.0f;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 81.0f;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *v =  [UIView new];
    v.backgroundColor = HEX_RGB(@"FFFFFF");
    return v;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //翻转动画
    ZYCCycleFriendModel *model = self.dataSource[indexPath.section];
    if (model.busFileList.count == 0) {
        ZYCCicleFriendsPlainDetailTextViewController *vc = [[ZYCCicleFriendsPlainDetailTextViewController alloc] init];
        vc.model = model;
        [UIView beginAnimations:@"View Flip"context:nil];
        [UIView setAnimationDuration:0.80];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO];
        [self.navigationController pushViewController:vc animated:YES];
        [UIView commitAnimations];
        
    }else{
        ZYCCicleFriendsDetailPhotoViewController *vc = [[ZYCCicleFriendsDetailPhotoViewController alloc] init];
        vc.model = model;
        vc.pageNum = self.pageNum;
        [UIView beginAnimations:@"View Flip"context:nil];
        [UIView setAnimationDuration:0.80];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO];
        [self.navigationController pushViewController:vc animated:YES];
        [UIView commitAnimations];
        
    }

}

#pragma mark - 网络请求
- (void)loadNewData{
    self.pageNum = 1;
    NSDictionary *params = @{
        @"pageSize":@(pageSize),
        @"pageNum":@(self.pageNum),
        @"stuUserId":self.model.userId
    };
    [MrjSVProgressHUD showWithStatus:@"加载中..."];
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_GrowthFileList requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(id  _Nullable responseObject) {
        [MrjSVProgressHUD dismiss];
        if (SUCCESS(responseObject[@"code"])) {
            [self.dataSource removeAllObjects];
            NSArray *array = [NSArray yy_modelArrayWithClass:[ZYCCycleFriendModel class] json:responseObject[@"body"][@"list"]];
            [self.dataSource addObjectsFromArray:array];
            [self getPositionWithArray:self.dataSource];
            [self.tableView showDataCount:self.dataSource.count];
            [self.tableView reloadData];
            // 每次下拉刷新需要重置一下
            [self reset];
            [self.tableView.mj_header endRefreshing];
        }else{
            [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
        }
    } failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
    }];
}


- (void)getPositionWithArray:(NSArray <ZYCCycleFriendModel *>*)array{
    NSMutableArray *temp = [NSMutableArray arrayWithCapacity:0];
    for (NSInteger i = 0 ; i < array.count; i++) {
        ZYCCycleFriendModel *model = array[i];
        NSString *dataString = [NSDateExtra getDateStringWithDateString:model.uploadTime currenDateFormat:@"yyyy-MM-dd HH:mm:ss" dateFormat:@"yyyy-MM-dd"];
        if (![temp containsObject:dataString]) {
            [temp addObject:dataString];
            model.isShow = YES;
        }
    }
}


-(void)loadMoreData{
    self.pageNum ++;
    NSDictionary *params = @{
        @"pageSize":@(pageSize),
        @"pageNum":@(self.pageNum),
        @"stuUserId":self.model.userId
    };
    [MrjSVProgressHUD showWithStatus:@"加载中..."];
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_GrowthFileList requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        } success:^(id  _Nullable responseObject) {
            [MrjSVProgressHUD dismiss];
            if (SUCCESS(responseObject[@"code"])) {
                NSArray *array = [NSArray yy_modelArrayWithClass:[ZYCCycleFriendModel class] json:responseObject[@"body"][@"list"]];
                if (array.count == 0) {
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }else{
                    [self.dataSource addObjectsFromArray:array];
                    [self getPositionWithArray:self.dataSource];
                    [self.tableView reloadData];
                    [self.tableView.mj_footer endRefreshing];
                }
            }else{
                [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
            }
        } failure:^(NSError * _Nonnull error) {
            [self.tableView.mj_footer endRefreshing];
        }];
}

- (void)reset{
    [self.tableView.mj_footer setRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [self.tableView.mj_footer resetNoMoreData];
}


#pragma mark - getters


- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, kScreenHeight -kNavBarAndStatusBarHeight) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        [_tableView registerClass:[ZYCCircleFriendsCell class] forCellReuseIdentifier:NSStringFromClass([ZYCCircleFriendsCell class])];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];

    }
    return _tableView;
}

- (ZYCGrowthFileHeaderView *)headerView {
    ZYCWeakSelf
    if (!_headerView) {
        _headerView = [[ZYCGrowthFileHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 130.0f)];
        _headerView.selectImageBlock = ^(ZYCGrowthFileModel * _Nonnull model) {
            ZYCPublishCircleFriendsViewController *vc = [[ZYCPublishCircleFriendsViewController alloc] init];
                vc.model = model;
                ZYCNavigationController *nav = [[ZYCNavigationController alloc] initWithRootViewController:vc];
                nav.modalPresentationStyle = UIModalPresentationFullScreen;
                nav.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
                [weakSelf presentViewController:nav animated:YES completion:nil];
        };
    }
    return _headerView;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSource;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
